package view;

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/**
 * Handles the logging in of a user as a tutor or
 * as an administrator.
 * 
 * @author Tristan D. Boucher
 * @version Commit #22 9/14/2013
 */
@SuppressWarnings("serial")
public class GUILoginPanel extends AbstractView
{
  //Constants
  /**
   * The Width of this Panel.
   */
  private static final int DEFAULT_WIDTH = 300;

  /**
   * The Height of this Panel.
   */
  private static final int DEFAULT_HEIGHT = 185;

  /**
   * The number of columns for the login panel.
   */
  private static final int DEFAULT_COLUMNS = 2;

  /**
   * The text size for this gui.
   */
  private static final int TEXT_SIZE = 16;

  /**
   * The default username that initializes the text_field.
   */
  private static final String DEFAULT_USERNAME = "Username";

  /**
   * The default password that initializes the text field.
   */
  private static final String DEFAULT_PASSWORD = "Password";

  /**
   * The generic label for Login button.
   */
  private static final String LOGIN_LABEL = "Login";

  /**
   * The generic label for the Quit button.
   */
  private static final String QUIT_LABEL = "Quit";

  //Fields
  /**
   * The text field in which a username will be entered
   * for logging into this application.
   */
  private JTextField my_username_field;

  /**
   * The text field in which the password will be entered
   * for logging into this application.
   */
  private JTextField my_password_field;

  /**
   * The button used to login.
   */
  private JButton my_login_button;

  /**
   * The usual quit/cancel button for UI standards.
   */
  private JButton my_quit_button;

  //Instance Methods
  /**
   * Constructor constructs this panel and readies it
   * for display.
   * 
   * @param the_controller The controller shared by all GUIs.
   */
  public GUILoginPanel(final GUIController the_controller)
  {
    super(the_controller, DEFAULT_WIDTH, DEFAULT_HEIGHT);

    initialize();
    
    this.setLayout(new BorderLayout());
    this.setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
    
    this.add(buildIntroPanel(), BorderLayout.NORTH);
    this.add(buildLabelFieldPanel(), BorderLayout.CENTER);
    this.add(buildButtonPanel(), BorderLayout.SOUTH);

    addLinedBorder(this, LINED_BORDER_COLOR);

  }

  /**
   * Helper method that instantiates all fields.
   */
  private void initialize()
  {
    my_username_field = new JTextField(DEFAULT_USERNAME, FIELD_LENGTH);
    my_password_field = new JTextField(DEFAULT_PASSWORD, FIELD_LENGTH);

    my_login_button = new JButton(LOGIN_LABEL);
    my_quit_button = new JButton(QUIT_LABEL);
  }
  
  /**
   * Builds the intro panel for the login.
   * @return The constructed introduction panel.
   */
  private JPanel buildIntroPanel()
  {
    final JPanel intro_panel = new JPanel();
    addLinedBorder(addBackground(intro_panel, BACKGROUND_COLOR), LINED_BORDER_COLOR);

    final JLabel intro_field = new JLabel("Please Sign In");
    intro_field.setFont(new Font(FONT, Font.ITALIC, TEXT_SIZE));
    intro_field.setHorizontalTextPosition(SwingConstants.CENTER);
    
    intro_panel.add(intro_field);
    
    return intro_panel;
  }
  
  /**
   * Builds The label field panel for the login.
   * @return The constructed field panel.
   */
  private JPanel buildLabelFieldPanel()
  {
    final JPanel label_field_panel = new JPanel();
    label_field_panel.setLayout(new GridLayout(0, DEFAULT_COLUMNS));
    label_field_panel.add(my_username_field);
    label_field_panel.add(my_password_field);
    
    addTitledBorder(addBackground(my_username_field, BACKGROUND_COLOR), 
                    DEFAULT_USERNAME, TEXT_FIELD_BORDER_COLOR, 
                    TEXT_SIZE).addKeyListener(new ConfirmKeyListener());
    addTitledBorder(addBackground(my_password_field, BACKGROUND_COLOR),
                    DEFAULT_PASSWORD,
                    TEXT_FIELD_BORDER_COLOR, 
                    TEXT_SIZE).addKeyListener(new ConfirmKeyListener());
    
    return label_field_panel;
  }
  
  /**
   * Builds the button panel for the login.
   * @return The constructed button panel.
   */
  private JPanel buildButtonPanel()
  {
    final JPanel button_panel = new JPanel();
    addBackground(button_panel, BACKGROUND_COLOR);
    addLinedBorder(button_panel, LINED_BORDER_COLOR);

    addBackground(my_login_button, BACKGROUND_COLOR);
    addBackground(my_quit_button, BACKGROUND_COLOR);

    button_panel.add(my_login_button);
    button_panel.add(my_quit_button);
    
    my_login_button.addActionListener(new LoginListener());
    my_quit_button.addActionListener(new QuitListener());
    
    return button_panel;
  }

  /**
   * Posts the login for whatever event needs it.
   * 
   * @author Tristan D. Boucher
   * @version Commit #9 8/27/2013
   */
  private void login()
  {
    if (!my_username_field.getText().trim().isEmpty() &&
        !my_password_field.getText().trim().isEmpty())
    {
      my_controller.postLogin(my_username_field.getText().trim(),
                              my_password_field.getText().trim());
    }
  }
  
  //Inner Listener Classes
  /**
   * Lots of work to get the enter button to register and
   * login.
   * 
   * @author Tristan D. Boucher
   * @version Commit #8 8/27/2013
   */
  private class ConfirmKeyListener implements KeyListener
  {  
    /**
     * Stupid required methods from interface.
     * 
     * @param the_e The event caused.
     */
    public void keyPressed(final KeyEvent the_e)
    {
      //Stupid required methods from interface.
    }

    /**
     * This overridden method posts the login for
     * an enter key released.
     * 
     * @param the_e The event caused.
     */
    public void keyReleased(final KeyEvent the_e)
    {
      if (the_e.getKeyCode() == KeyEvent.VK_ENTER)
      {
        login(); 
      }
    }

    /**
     * Stupid required methods from interface.
     * 
     * @param the_e The event caused.
     */
    public void keyTyped(final KeyEvent the_e)
    {
      //Stupid required methods from interface.
    }
  }

  /**
   * Action listener for the login button.
   * 
   * @author Tristan D. Boucher
   * @version Commit #7 8/26/2013
   */
  private class LoginListener implements ActionListener
  {
    /**
     * Overridden actionPerformed method for listener.
     * 
     * @param the_e The event that triggered this method
     * to operate.
     */
    public void actionPerformed(final ActionEvent the_e)
    {
      login();
    }
  }

  /**
   * Action listener for the quit button.
   * 
   * @author Tristan D. Boucher
   * @version Commit #8 8/26/2013
   */
  private class QuitListener implements ActionListener
  {
    /**
     * Overridden actionPerformed method for listener.
     * 
     * @param the_e The event that triggered this method
     * to operate.
     */
    public void actionPerformed(final ActionEvent the_e)
    {
      my_controller.postQuit();
    }
  }
}
//End of class GUILoginPanel